import AppSettings from "@runtime/base/AppSettings";
import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";
import AssetLoaderComponent from "@runtime/UnityEngineBridge/AssetLoaderComponent";
import { Ref, ref, WatchStopHandle } from "@vue/runtime-dom";
import { message } from 'ant-design-vue';
import MessageCode from "./MessageCode";
import OrbitCameraComponent from "./OrbitCameraComponent";
import statComponen from "./StateCode";

/**
 * 起始页
 */
export default class HomeContent {
    private static instance: HomeContent = new HomeContent();
    /**
     * 起始页
     */
    public static get Default(): HomeContent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);

        for (const watchStopHandle of this.watchStopHandles) {
            watchStopHandle();
        }
        this.watchStopHandles.clear();
    }


    public Loaded: Ref<boolean> = ref(true);

    private watchStopHandles: Array<WatchStopHandle> = [];

    public ProgressText: Ref<string> = ref('');

    public ModelID: Ref<Array<any>> = ref([]);
    public QRList: Ref<Array<any>> = ref([]);

    public ProgressVal: Ref<number> = ref(0);

    public QRType: Ref<boolean> = ref(false);
    public VideoList: Ref<boolean> = ref(false);

    public VideoBox: Ref<boolean> = ref(false);
    //视频地址
    public VideoAddress: Ref<string> = ref('');
    //模块类型
    public ModuleType: Ref<number> = ref(0)
    //幻灯片页码
    public SlideNum: Ref<number> = ref(0)
    // 首页发动机系统
    public engine: Ref<boolean> = ref(false);
    // 首页灯光系统
    public lamplight: Ref<boolean> = ref(false);
    // 首页发送机和灯光系统的盒子
    public show: Ref<boolean> = ref(true);
    // 首页大盒子（包括左右箭头）
    public bigBox: Ref<boolean> = ref(true);


    // 资源路径存储
    public AssetList: Ref<Array<any>> = ref([]);
    public sourseId: Ref<any> = ref(new Map([]))
    //数据监听
    public OnReceive(id: number, content: string): void {
        console.log(`${id} - ${content}`);
        if (this.IsJsonString(content)) {
            console.log(JSON.parse(content));
            if (id == MessageCode.Default.MsgCodesList.DIAGNOSTIC_APPEARENCE_UPDATE_VIEW) {
                //UI更新现象
                JSON.parse(content).Result.forEach((item: any) => {
                    statComponen.Default.StatusConfigs.value.set(item.Id, item.Value)
                });
            }
        }
    }
    //判断类型
    IsJsonString(str: any) {
        try {
            JSON.parse(str);
        } catch (e) {
            return false;
        }
        return true;
    }

    public async Awake(): Promise<void> {
        this.Loaded.value = true;


        // 启动Unity
        // 资源编号R00052
        const configuration = JSON.stringify({
            LocalAddress: AppSettings.Default.LocalAddress,
            ShortName: 'P10072',
            Type: 13,
        });

        await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.EmbeddedApplicationLaunchRequest('../EmbeddedApplication/Unity/Mix.Unity.exe', configuration));


        // // 获取Unity版本状态
        await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.EmbeddedApplicationVersionRequest());



        // let viewArr: any = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DiagnosisAdapterRequest(MessageCode.Default.MsgCodesList.VIEW_DATA_CONFIG, '1'))
        // let ValData = JSON.parse(viewArr.Content).Result
        // await OrbitCameraComponent.Default.UpdateV2Async(ValData.TargetContainer, ValData.Horizontal, ValData.Vertical, ValData.Distance, ValData.MinDistance, ValData.MaxDistance, ValData.VerticalAngleMax, ValData.VerticalAngleMin, 1, 1, 1, ValData.HorizontalAngleMax, ValData.HorizontalAngleMin);
        // await OrbitCameraComponent.Default.UpdateV2Async('汽车维修4S店/整车模块', 1, 45, 2.0, 0.3, 3.0, 1, 1, 1);
    }

    public async Destroy(): Promise<void> {
    }
}